package com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel

import androidx.lifecycle.MutableLiveData
import com.vitabuybuy.tv.dto.TalentUserInfoDto
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.core.databinding.BooleanObservableField
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.ApplyTalentRepository
import com.vitabuybuy.tv.network.AuthRepository
import java.util.HashMap

class UserCenterViewModel : BaseViewModel() {
    val isFirstLoading = BooleanObservableField()
    val applyTalentUserInfo = MutableLiveData<TalentUserInfoDto>()
    fun getTalentUserInfo() {
        rxHttpRequest {
            onRequest = {
                applyTalentUserInfo.value = ApplyTalentRepository.applyTalentUserInfo().await()
                statusView.set(LoadingType.SHOW_CONTENT)
                isFirstLoading.set(false)
            }
            onError = {
                XToast.normal(it.msg)
                isFirstLoading.set(true)
            }
            isLoadingType = true
            isRefresh = isFirstLoading.get()
        }
    }

    fun uploadImage(path: String) {
        rxHttpRequest {
            onRequest = {
              val it =  AuthRepository.upLoadImage(path) .await()
                defUI.msgEvent.postValue(MessageModel(2, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getRegionList(regionId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = AddressRepository.getRegionList(regionId).await()
                defUI.msgEvent.postValue(MessageModel(1, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun updateTalent(executorMap: HashMap<String, Any>) {
        rxHttpRequest {
            onRequest = {
                ApplyTalentRepository.updateTalent(executorMap) .await()
                defUI.msgEvent.postValue(MessageModel(3))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun resetCase(id: Int?, content: String, caseImage: String) {
        rxHttpRequest {
            onRequest = {
                ApplyTalentRepository.resetCase(id, content, caseImage).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun addCase(content: String, caseImage: String) {
        rxHttpRequest {
            onRequest = {
                ApplyTalentRepository.addCase(content, caseImage).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun deleteCase(id: Int,position: Int) {
        rxHttpRequest {
            onRequest = {
                ApplyTalentRepository.deleteCase(id) .await()
                defUI.msgEvent.postValue(MessageModel(1,arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
